//
//  FilterViewCell.m
//  Finance
//
//  Created by cheng xiang on 13-8-5.
//  Copyright (c) 2013年 cheng xiang. All rights reserved.
//

#import "FilterViewCell.h"

@implementation FilterViewCell
@synthesize title = _title, delegate = _delegate,array = _array;
@synthesize select1View = _select1View, select2View = _select2View, selectallView = _selectallView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.textLabel.font = [UIFont boldSystemFontOfSize:15];
    }
    return self;
}

- (void) drawCell:(NSArray *)array withLimit:(BOOL)limit
{
    self.array = array;
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.tag = 1;
    [btn1 addTarget:self action:@selector(clickOnNotLimit:) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbtnbg.png"]];
    bgView.frame = CGRectMake(3.5, 3.5, 25, 25);
    
    _select1View = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"select.png"]];
    _select1View.frame = CGRectMake(3.5, 3.5, 16, 16);
    [bgView addSubview:_select1View];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(32, 0, 60, 32)];
    title.font = [UIFont systemFontOfSize:13];
    title.text = [array count] > 0 ? [array objectAtIndex:0] : @"";
    [title setBackgroundColor:[UIColor clearColor]];

    [btn1 addSubview:title];
    [btn1 addSubview:bgView];
    [self addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.tag = 2;
    [btn2 addTarget:self action:@selector(clickOnNotLimit:) forControlEvents:UIControlEventTouchUpInside];
    bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbtnbg.png"]];
    bgView.frame = CGRectMake(3.5, 3.5, 25, 25);
    
    _select2View = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"select.png"]];
    _select2View.frame = CGRectMake(3.5, 3.5, 16, 16);
    [bgView addSubview:_select2View];
    
    title = [[UILabel alloc] initWithFrame:CGRectMake(32, 0, 60, 32)];
    title.font = [UIFont systemFontOfSize:13];
    title.text = [array count] > 1 ? [array objectAtIndex:1] : @"";
    [title setBackgroundColor:[UIColor clearColor]];
    [btn2 addSubview:title];
    
    [btn2 addSubview:bgView];
    [self addSubview:btn2];
    
    if (limit) {
        UIButton *limitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        limitBtn.frame = CGRectMake(106, 7, 100, 32);
        [limitBtn addTarget:self action:@selector(clickOnLimit:) forControlEvents:UIControlEventTouchUpInside];
        
        _selectallView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selectAll.png"]];
        _selectallView.frame = CGRectMake(3.5, 3.5, 25, 25);
        [limitBtn addSubview:_selectallView];
        
        title = [[UILabel alloc] initWithFrame:CGRectMake(32, 0, 60, 32)];
        title.font = [UIFont systemFontOfSize:13];
        title.text = @"不限";

        [title setBackgroundColor:[UIColor clearColor]];
        [limitBtn addSubview:title];
        
        [self addSubview:limitBtn];
        
        btn1.frame = CGRectMake(190, 7, 100, 32);
        btn2.frame = CGRectMake(106, 51, 100, 32);
        _select1View.hidden = YES;
        _select2View.hidden = YES;
    }else{
        btn1.frame = CGRectMake(106, 7, 100, 32);
        btn2.frame = CGRectMake(190, 7, 100, 32);
        _select1View.hidden = NO;
        _select2View.hidden = YES;
    }
}

- (void)clickOnLimit:(id)sender
{
    _selectallView.image = [UIImage imageNamed:@"selectAll.png"];
    _select1View.hidden = YES;
    _select2View.hidden = YES;
    
    if ([_delegate respondsToSelector:@selector(textEditDone:forKey:)]) {
        [_delegate performSelector:@selector(textEditDone:forKey:) withObject:@"不限" withObject:self.title];
    }
}

- (void)clickOnNotLimit:(id)sender
{
    int tag = ((UIButton *)sender).tag;
    if (tag == 1) {
        _selectallView.image = [UIImage imageNamed:@"notselectAll.png"];
        _select1View.hidden = NO;
        _select2View.hidden = YES;
    }else{
        _selectallView.image = [UIImage imageNamed:@"notselectAll.png"];
        _select1View.hidden = YES;
        _select2View.hidden = NO;
    }
    
    NSString *text = [_array count] > tag-1 ? [_array objectAtIndex:tag-1] : @"不限";
    if ([_delegate respondsToSelector:@selector(textEditDone:forKey:)]) {
        [_delegate performSelector:@selector(textEditDone:forKey:) withObject:text withObject:self.title];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
